package com.ama.controller;

import com.ama.dao.RoomDao;
import com.ama.pojo.Build;
import com.ama.pojo.Estate;
import com.ama.pojo.Room;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class RoomManageController {
    @Autowired
    RoomDao roomDao;
    @RequestMapping("/addRoom")
    public String addRoom(HttpServletRequest request){
        Long estateId = Long.valueOf(request.getParameter("estateId"));
        Long buildId = Long.valueOf(request.getParameter("buildId"));
        Long roomId = Long.valueOf(request.getParameter("roomId"));
        String name = request.getParameter("roomName");
        String roomFloor = request.getParameter("roomFloor");
        double roomEffectarea = Double.parseDouble(request.getParameter("roomEffectarea"));
        double roomRentarea = Double.parseDouble(request.getParameter("roomRentarea"));
        double roomPublicarea = Double.parseDouble(request.getParameter("roomPublicarea"));
        double roomPrice = Double.parseDouble(request.getParameter("roomPrice"));
        int bednum = Integer.parseInt(request.getParameter("bednum"));
        String roomuse = request.getParameter("roomuse");
        int room_state = Integer.parseInt(request.getParameter("room_state"));
        Integer room_open = Integer.getInteger(request.getParameter("room_open"));
        String roomInfo = request.getParameter("roomInfo");

        Room room = new Room();
        room.setRoomId(roomId);
        room.setEstateId(estateId);
        room.setBuildId(buildId);
        room.setRoomName(name);
        room.setRoomFloor(roomFloor);
        room.setRoomEffectarea(roomEffectarea);
        room.setRoomRentarea(roomRentarea);
        room.setRoomPublicarea(roomPublicarea);
        room.setRoomPrice(roomPrice);
        room.setBednum(bednum);
        room.setRoomUse(roomuse);
        room.setRoomState(room_state);
        room.setRoomOpen(room_open);
        room.setRoomInfo(roomInfo);

        roomDao.insert(room);

        return "redirect:/success/addBuildManage_suc.jsp";
    }

    @RequestMapping("/openRoom")
    public String openEstate(HttpServletRequest request) {
        Long roomId = Long.valueOf(request.getParameter("room"));
        Room room = roomDao.selectById(roomId);
        room.setRoomOpen(1);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("room_id", roomId);
        roomDao.update(room, wrapper);
        return "redirect:/pro_page/p_estatemanage.jsp";

    }

    @RequestMapping("/banRoom")
    public String banEstate(HttpServletRequest request) {
        Long roomId = Long.valueOf(request.getParameter("room"));
        Room room = roomDao.selectById(roomId);
        room.setRoomOpen(-1);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("room_id", roomId);
        roomDao.update(room, wrapper);

        return "redirect:/pro_page/p_estatemanage.jsp";

    }

    @RequestMapping("/delRoom")
    public String delEstate(HttpServletRequest request) {
        Long roomId = Long.valueOf(request.getParameter("room"));
        roomDao.deleteById(roomId);
        return "redirect:/pro_page/p_estatemanage.jsp";
    }


}
